<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 订单模型类
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-6
 * Time: 上午9:38
 */
class Order_Orderinfo_model extends CI_Model
{

    private $sid = '';
    private $table_line;
    private $table_line_go;
    private $table_line_info;
    private $table_supplier;
    private $table_order_go;
    private $table_order_go_price;

    private $table_go_pay;
    private $table_admin_user;
    private $table_division;
    private $table_order_pay;
    private $table_hetong;
    private $table_order_tourist;

    private $table_order_hotel;
    private $table_order_contacts;
    private $table_order_info;
    private $order_status_array;
    private $pay_status_array;



    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->table_order_go = $this->db->dbprefix('order_go_date');
        $this->table_order_go_price = $this->db->dbprefix('order_go_date_price');
        $this->table_order_pay = $this->db->dbprefix('order_pay_log');

        $this->table_line = $this->db->dbprefix('line');
        $this->table_line_go = $this->db->dbprefix('line_go');
        $this->table_line_info = $this->db->dbprefix('line_info');
        $this->table_supplier = $this->db->dbprefix('line_supplier');
        $this->table_go_pay = $this->db->dbprefix('go_pay_log');
        $this->table_admin_user = $this->db->dbprefix('admin_user');
        $this->table_division = $this->db->dbprefix('division');
        $this->table_hetong = $this->db->dbprefix('hetong');
        $this->table_order_tourist = $this->db->dbprefix('order_tourist');

        $this->table_order_info = $this->db->dbprefix('order_info');
        $this->table_order_hotel = $this->db->dbprefix('order_hotel');
        $this->table_order_contacts = $this->db->dbprefix('order_contacts');


        $this->load->model('order.Orderpaylog_model');
        $this->load->model('admin.Adminuser_model');
        $this->load->driver('cache');

        $this->config->load('base');
        $this->order_status_array = $this->config->item('order_status');
        $this->pay_status_array = $this->config->item('pay_status');

    }


    /**
     * 用户中心获取酒店订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_index_hotel_list($page_size = 20, $now_page = 1, $where)
    {
        $this->db->select('*');
        $this->db->join($this->table_order_contacts, $this->table_order_contacts . ".order_id=$this->table_order_info.order_id", 'inner');
        $this->db->join($this->table_order_hotel, $this->table_order_hotel . ".order_id=$this->table_order_info.order_id", 'inner');

        if (!empty($where)) $this->db->where($where, null, false);
        $this->db->order_by($this->table_order_info . '.add_time', 'DESC');
        $query = $this->db->get($this->table_order_info, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row)
        {
            $row['edit_url'] = site_url('order/index/ticket_info?id=' . $row['order_id']);
            $row['order_status_name'] = $this->order_status_array[$row['order_status']];
            $row['pay_status_name'] = $this->pay_status_array[$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];
            $row['scenic_url'] = site_url('jingdian/' . $row['scenic'] . '.html');

            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1)
            {
                $row['verify_url'] = site_url('order/admin/ajax_order_status?id=' . $row['order_id']);
            }
            if ($row['order_status'] != 1 && $row['pay_status'] != 3)
            {
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
            }
            $data[] = $row;
        }
        return $data;
    }


    /**
     * 更新数据
     * @param array $data 数组
     * @param mixed $where 查询条件 array|string
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data, $where)
    {
        $table = $this->_get_table_name();
        if(!$where['sid']){
            $where['sid'] = $GLOBALS['sid'];
        }
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        return $this->db->update($table, $data);
    }

    /**
     * 订单调账
     * @param $order_id
     * @param int $account
     * @return mixed
     */
    public function order_tz($order_id, $account = 0,$pay_status = 2)
    {
        $table = $this->_get_table_name();
        if ($account >= 0) {
            $sql = "update $table set order_account=order_account+$account,pay_status=$pay_status where order_id=$order_id";
        } else {
            $sql = "update $table set order_account=order_account$account,pay_status=$pay_status where order_id=$order_id";
        }
        return $this->db->query($sql);
    }

    /**
     * 添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data)
    {
        $this->load->helper('cookie');
        $admin_id = empty($data['admin_user_id']) ? intval(get_cookie('from_card')) : $data['admin_user_id'];
        if ($admin_id) {
            $data['admin_user_id'] = $admin_id;
        }
        if(!$data['sid']){
            $data['sid'] = $GLOBALS['sid'];
        }
        $table = $this->_get_table_name();
        if (is_array($data)) {
            if ($this->db->insert($table, $data)) {
                return $this->db->insert_id();
            } else {
                return false;
            }
        }
        return false;
    }


    public function fetch_count($date_array, $cat_type = array(), $order_type = array() )
    {
        foreach ($date_array as $k => $v) {
            $ret[$k] = $this->fetch_count_by_date($v, $date_array[$k+1], $cat_type, $order_type);
        }
        return $ret;
    }


    public function fetch_count_by_date($start, $end, $cat_type = array(), $order_type = array())
    {
        $table = $this->_get_table_name();
        $hotel_table = $this->db->dbprefix("order_hotel");;

        if($end){
            $end = " AND $hotel_table.start_date<='$end' ";
        }
        if($cat_type){
            $cat_type = " AND $table.cat_type IN(".implode($cat_type).") ";
        }else{
            $cat_type = '';
        }
        if($order_type){
            $order_type = " AND $table.order_type IN(".implode($order_type).") ";
        }else{
            $order_type = '';
        }
        $start = addslashes($start);
//        $key = md5(serialize(array_merge($start .$end, $cat_type, $order_type .'_')));
//        $cache_key = '_'.$key.SITEID;
//
//        $ret = $this->cache->file->get($cache_key);
//        if(!$ret || !$end) {

            $sql = "SELECT COUNT(*) AS c FROM $table LEFT JOIN $hotel_table on $table.order_id = $hotel_table.order_id WHERE $table.sid=" . SITEID . " $cat_type $order_type AND $hotel_table.start_date LIKE '$start%'";
            $query = $this->db->query($sql);
            $data = $query->result_array();
            $ret = intval($data[0]['c']);
//            $this->cache->file->save($cache_key, $ret,864000000);
//        }

        return $ret;
    }

    /**
     * 查询单条记录
     * @param mixed $where 查询条件 array|string
     * @param string $field 获取到的字段
     * @return mixed 返回数据
     */
    public function get_row($where, $field = '*')
    {
        $table = $this->_get_table_name();
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        if (!empty($field)) $this->db->select($field);
        $query = $this->db->get($table, 1);
        $row = $query->row_array();
        if (!empty($row)) {
            if (isset($row['order_status'])) $row['order_status_name'] = $this->order_status_array[$row['order_status']];
            if (isset($row['pay_status']))  $row['pay_status_name'] = $this->pay_status_array[$row['pay_status']];
        }
        return $row;
    }

    /**
     * 删除内容
     * @param mixed $where 查询条件 array|string
     * @return bool 返回数据
     */
    public function remove_row($where)
    {
        $table = $this->_get_table_name();
        if (!empty($where)) return $this->db->delete($table, $where);
        return false;
    }

    /**
     * 设置订单流水号
     * @return string
     */
    public function set_order_sn()
    {
        return date('YmdHis', now()) . rand(1000, 9999);
    }

    /**
     * 根据session信息获取订单的概况信息
     */
    public function get_session_info()
    {
        $this->db->select('line_name');
        $order_session = $this->session->userdata('order');
        $this->db->where(array('line_id' => $order_session['line_id']));
        $query = $this->db->get($this->db->dbprefix("line"), 1);
        $line = $query->row_array();

        $this->db->select('go_date,dif_price');
        $this->db->where(array('go_id' => $order_session['go_id']));
        $query = $this->db->get($this->db->dbprefix("line_go"), 1);
        $go_date = $query->row_array();

        $order = array();
        $order['line_name'] = $line['line_name'];
        $order['go_date'] = $go_date['go_date'];
        $order['dif_price'] = $go_date['dif_price'];
        $price_cate = $order_session['price_cate'];
        $order['financial_account'] = $order['dif_price'];//财务支付金额 = 价格+单房差
        $financial_account = 0;
        $order_account = 0;
        if (!empty($price_cate)) {
            $price = array();
            $this->db->select('price_id,price_name,ms_price,th_price,ws_price');
            foreach ($price_cate as $key => $val) {
                $this->db->where(array('price_id' => $key));
                $query = $this->db->get($this->db->dbprefix("go_price"), 1);
                $go_price = $query->row_array();
                $row = array(
                    'price_id' => $go_price['price_id'],
                    'price_name' => $go_price['price_name'],
                    'price' => $go_price['ms_price'],
                    'th_price' => $go_price['th_price'],
                    'num' => $val,
                );
                $order_account += $row['price'] * $row['num'];
                $financial_account += $row['th_price'] * $row['num'];
                $price[] = $row;
            }
            $order['price'] = $price;
            $order['order_account'] = $order_account;
            $order['financial_account'] += $financial_account;
        }
        return $order;
    }

    /**
     *
     * 门市添加订单，获取订单相关信息
     */
    public function get_order_info($data)
    {
        $this->db->select('line_name, line_id, oid');
        $this->db->where(array('line_id' => $data['line_id']));
        $query = $this->db->get($this->db->dbprefix("line"), 1);
        $line = $query->row_array();

        $this->db->select('go_date,dif_price');
        $this->db->where(array('go_id' => $data['go_id']));
        $query = $this->db->get($this->db->dbprefix("line_go"), 1);
        $go_date = $query->row_array();

        $order = array();
        $order['line_name'] = $line['line_name'];
        $order['line_id'] = $line['line_id'];
        $order['oid'] = $line['oid'];
        $order['go_date'] = $go_date['go_date'];
        $order['dif_price'] = $go_date['dif_price'];
        $price_cate = $data['price_cate'];
        $cb_account = 0;
        $order['financial_account'] = 0;//$order['dif_price'];//财务支付金额 = 价格+单房差
        $financial_account = 0;
        $order_account = 0;
        $fee_account = 0;//佣金
        if (!empty($price_cate)) {
            $price = array();
            $this->db->select('price_id,price_name,ms_price,th_price,cb_price,ws_price,cate_attr,fee');
            foreach ($price_cate as $key => $val) {
                $this->db->where(array('price_id' => $key));
                $query = $this->db->get($this->db->dbprefix("go_price"), 1);
                $go_price = $query->row_array();
                $row = array(
                    'price_id' => $go_price['price_id'],
                    'price_name' => $go_price['price_name'],
                    'price' => $go_price['ms_price'],
                    'th_price' => $go_price['th_price'],
                    'cb_price' => $go_price['cb_price'],
                    'cate_attr' => $go_price['cate_attr'],
                    'fee'=>$go_price['fee'],
                    'num' => $val,
                );
                $cb_account += $row['cb_price'] * $row['num'];
                $order_account += $row['price'] * $row['num'];
                $financial_account += $row['th_price'] * $row['num'];
                $fee_account += $row['fee'] * $row['num'];
                $price[] = $row;
            }
            $order['price'] = $price;
            $order['fee_account'] = $fee_account;
            $order['order_account'] = $order_account;
            $order['financial_account'] += $financial_account;
            $order['cb_account'] = $cb_account;
        }
        return $order;
    }

    /**
     * 获取订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_list($page_size = 20, $now_page = 1, $where)
    {

        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $go_date_table = $this->db->dbprefix("order_go_date");
        $line_table = $this->db->dbprefix('line');
        $this->db->select($go_date_table . '.go_id,' . $table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,'. $table . '.paid_account,' . $table . '.order_status,' . $table . '.add_time,' . $table . '.user_id,' . $table . '.admin_user_id,' . $table . '.kouwei,' . $table . '.finacial_status,' . $table . '.from_sid,' . $table . '.receipt,' . $table . '.receipt_account,' . $table . '.receipt_status');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($go_date_table . '.go_date,' . $go_date_table . '.line_name,' . $go_date_table . '.total_num,' . $go_date_table . '.line_number,' . $go_date_table . '.financial_status');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($go_date_table, $go_date_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($line_table, $line_table . ".line_id=$go_date_table.line_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/admin/line_sales_edit?id=' . $row['order_id']);
            if ($row['cat_type'] == 8) {
                $row['edit_url'] = site_url('order/admin/dis_edit?id=' . $row['order_id']);
            }
            if ($row['from_sid'] && ($row['from_sid'] != $this->sid)) {
                $row['edit_url'] = site_url('order/admin/fx_order_edit?id=' . $row['order_id']);
            }
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];

            $paid_account = $row['paid_account'];
            $row['paid_account'] = $this->Orderpaylog_model->get_pay_account(array('order_id' => $row['order_id'], 'status' => 2));
            $row['paid_account'] = $row['paid_account'] ? $row['paid_account'] : $paid_account;

            $yukuan = $row['order_account'] - $row['paid_account'];
//if(DEBUG){
//    print_r($row);
//    echo PHP_EOL;
//}
            switch ($yukuan) {
                case 0:
                    $row['pay_status'] = 3;
                    break;
                case $row['order_account']:
                    $row['pay_status'] = 1;
                    break;
                default:
                    $row['pay_status'] = 2;
                    break;
            }

            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {//审核订单
                $row['verify_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=2&go_id='.$row['go_id'].'&total_num='.$row['total_num']);
            }

            if ($row['order_status'] == 2) {//支付订单
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
                $row['hetong_url'] = site_url('order/admin/hetong?id=' . $row['order_id']);
            }

            if ($row['pay_status'] == 1 && $row['order_status'] != 3) {//取消订单
                $row['cancel_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=3&go_id='.$row['go_id'].'&total_num='.$row['total_num']);
            }

            if ($row['order_status'] == 2 && $row['pay_status'] == 3) {//完成订单
                $row['finish_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=4&go_id='.$row['go_id']);
            }


            $data[] = $row;
        }
        return $data;
    }

    /**
     *
     * 获取订单列表
     * @param int $page_size
     * @param int $now_page
     * @param string $where
     */
    public function get_list_m($filter)
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $go_date_table = $this->db->dbprefix("order_go_date");
        $where = " $table.order_type=1 and $table.admin_user_id=" . $filter['admin_user_id'];
        if ($filter['keywords']) {
            $where .= " and $go_date_table.line_name like '%" . $filter['keywords'] . "%' ";
        }

        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($go_date_table . '.go_date,' . $go_date_table . '.line_name');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($go_date_table, $go_date_table . ".order_id=$table.order_id", 'inner');

        $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($table, $filter['page_size'], ($filter['page'] - 1) * $filter['page_size']);
        $data = array();
        $status = $this->_set_order_status_name();

        foreach ($query->result_array() as $row) {
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            unset($row['pay_status']);
            unset($row['order_status']);
            $data[] = $row;
        }
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($go_date_table . '.go_date,' . $go_date_table . '.line_name');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($go_date_table, $go_date_table . ".order_id=$table.order_id", 'inner');
        $this->db->where($where, NULL, FALSE);
        //总数
        $total = $this->db->get($table)->num_rows();

        if (!empty($data)) {
            $res['error_code'] = 0;
            $res['error_msg'] = '';
            $res['total'] = $total;
            $res['data'] = $data;
        } else {
            $res['error_code'] = 1;
            $res['error_msg'] = '暂时没有数据信息';
            $res['data'] = '';
            $res['total'] = 0;
        }
        return $res;
    }

    /**
     * 获取线路订单总数量
     * @param string $where 查询条件
     * @return mixed
     */
    public function get_count($where = '')
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $go_date_table = $this->db->dbprefix("order_go_date");
        $line_table = $this->db->dbprefix('line');
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($go_date_table . '.go_date,' . $go_date_table . '.line_name,' . $go_date_table . '.total_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($go_date_table, $go_date_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($line_table, $line_table . ".line_id=$go_date_table.line_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }

    /**
     * 获取门票订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_ticket_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.msg_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/adminticket/edit?id=' . $row['order_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];

            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {//审核订单
                $row['verify_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=2');
            }

            if ($row['order_status'] == 2 && $row['pay_status'] != 3) {//支付订单
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
            }

            if ($row['pay_status'] == 1 && $row['order_status'] != 3) {//取消订单
                $row['cancel_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=3');
            }

            if ($row['order_status'] == 2 && $row['pay_status'] == 3) {//完成订单
                $row['finish_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=4');
            }

            $data[] = $row;
        }
        return $data;
    }


    /**
     * 获取门票订单总数量
     * @param string $where 查询条件
     * @return mixed
     */
    public function get_ticket_count($where = '')
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }

    /**
     * 获取商品订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_goods_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $distribution_table = $this->db->dbprefix("order_distribution");
        $order_goods_table = $this->db->dbprefix("order_goods");
        $goods_table = $this->db->dbprefix("goods");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($distribution_table . '.district_name,' . $distribution_table . '.address,' . $goods_table . '.goods_name');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($distribution_table, $distribution_table . ".order_id=$table.order_id", 'LEFT');
        $this->db->join($order_goods_table, $order_goods_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($goods_table, $order_goods_table . ".goods_id=$goods_table.goods_id", 'LEFT');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/adminfeaturegoods/edit?id=' . $row['order_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];

            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {//审核订单
                $row['verify_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=2');
            }

            if ($row['order_status'] == 2 && $row['pay_status'] != 3) {//支付订单
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
            }

            if ($row['pay_status'] == 1 && $row['order_status'] != 3) {//取消订单
                $row['cancel_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=3');
            }

            if ($row['order_status'] == 2 && $row['pay_status'] == 3) {//完成订单
                $row['finish_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id'] . '&status=4');
            }

            $data[] = $row;
        }
        return $data;
    }


    /**
     * 获取商品订单总数量
     * @param string $where 查询条件
     * @return mixed
     */
    public function get_goods_count($where = '')
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $distribution_table = $this->db->dbprefix("order_distribution");
        $order_goods_table = $this->db->dbprefix("order_goods");
        $goods_table = $this->db->dbprefix("goods");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($distribution_table . '.district_name,' . $distribution_table . '.address');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($distribution_table, $distribution_table . ".order_id=$table.order_id", 'LEFT');
        $this->db->join($order_goods_table, $order_goods_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($goods_table, $order_goods_table . ".goods_id=$goods_table.goods_id", 'LEFT');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }

    /**
     * 用户中心获取订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_index_line_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $go_date_table = $this->db->dbprefix("order_go_date");
        $this->db->select($table . '.*');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($go_date_table . '.go_date,' . $go_date_table . '.line_name,' . $go_date_table . '.line_id,' . $go_date_table . '.total_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($go_date_table, $go_date_table . ".order_id=$table.order_id", 'inner');
        if($where){
            $where .= ' AND '.$go_date_table.'.sid='. SITEID . ' ';
        }else{
            $where .= ' '.$go_date_table.'.sid='. SITEID . ' ';
        }
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/index/line_info?id=' . $row['order_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];
            $row['xianlu_url'] = site_url('xianlu/' . $row['line_id'] . '.html');
            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {
                $row['verify_url'] = site_url('order/admin/ajax_order_status?id=' . $row['order_id']);
            }
            if ($row['order_status'] != 1 && $row['pay_status'] != 3) {
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
            }
            $data[] = $row;
        }
        return $data;
    }


    /**
     * 用户中心获取订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_index_flight_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $flight_table = $this->db->dbprefix("order_flight");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->select($flight_table . '.pay_money,' . $flight_table . '.pcount,' . $flight_table . '.grow_discount,' . $flight_table . '.tax,' . $flight_table . '.price,' . $flight_table . '.return,' . $flight_table . '.change,' . $flight_table . '.status,' . $flight_table . '.pnr,' . $flight_table . '.cabin,' . $flight_table . '.sairport,' . $flight_table . '.eairport,' . $flight_table . '.pname,' . $flight_table . '.flight,' . $flight_table . '.date,' . $flight_table . '.stime,' . $flight_table . '.etime,' . $flight_table . '.scityname,' . $flight_table . '.ecityname');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($flight_table, $flight_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/index/flight_info?id=' . $row['order_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['flight_status_name'] = $this->flight_model->return_status($row['status']);

            // 取消订单
            if ($row['order_status'] == 2 && $row['pay_status'] == 1) {
                $row['cancel_url'] = site_url('order/adminflight/ajax_order_status?pnr=' . $row['pnr'] . '&order_id=' . $row['order_id'] . '&order_sn=' . $row['order_sn'] . '&status=3');
            }

            if ($row['order_status'] == 2 && $row['pay_status'] == 1) {
                $row['pay_url'] = site_url('order/adminflight/ajax_order_pay?pnr=' . $row['pnr'] . '&order_id=' . $row['order_id'] . '&order_sn=' . $row['order_sn']);
            }

            if ($row['status'] == 2) {
                $row['refund_url'] = site_url('order/adminflight?pnr=' . $row['pnr'] . '&order_sn=' . $row['order_sn']);
            }

            $data[] = $row;
        }
        return $data;
    }

    /**
     * 获取线路订单总数量
     * @param string $where 查询条件
     * @return mixed
     */
    public function get_flight_count($where = '')
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $order_flight_table = $this->db->dbprefix("order_flight");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($order_flight_table, $order_flight_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }

    /**
     *
     * 统计总计结果
     * @param $where
     */
    function flight_order_list_total()
    {
        $table = $this->_get_table_name();
        $flight_table = $this->db->dbprefix("order_flight");
        $all_total = 0;
        $where = "" . $table . ".order_type=4 AND " . $table . ".sid=" . $this->sid . " AND " . $flight_table . ".status = 2 AND " . $table . ".order_status != 3 ";
        $this->db->select($flight_table . '.pay_money,' . $flight_table . '.pcount,' . $flight_table . '.grow_discount,' . $flight_table . '.tax,' . $flight_table . '.price');
        $this->db->join($flight_table, $flight_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($table);
        foreach ($query->result_array() as $key => $val) {
            $all_total += ($val['price'] * $val['pcount']) - ($val['pay_money'] - ($val['tax'] * $val['pcount']));

        }
        return intval($all_total);
    }

    /**
     * 用户中心获取门票订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_index_ticket_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $order_ticket_table = $this->db->dbprefix("order_tickets");
        $ticket_table = $this->db->dbprefix("ticket");
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($ticket_table . '.name,' . $ticket_table . '.scenic_name,' . $ticket_table . '.scenic,' . $ticket_table . '.basic_type,' . $ticket_table . '.category,' . $ticket_table . '.payment_method,' . $order_ticket_table . '.user_date,' . $order_ticket_table . '.num');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($table, $table . ".order_id=$order_ticket_table.order_id", 'inner');
        $this->db->join($ticket_table, $ticket_table . ".id=$order_ticket_table.ticket_id", 'inner');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($order_ticket_table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/index/ticket_info?id=' . $row['order_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];
            $row['scenic_url'] = site_url('jingdian/' . $row['scenic'] . '.html');

            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {
                $row['verify_url'] = site_url('order/admin/ajax_order_status?id=' . $row['order_id']);
            }
            if ($row['order_status'] != 1 && $row['pay_status'] != 3) {
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
            }
            $data[] = $row;
        }
        return $data;
    }

    /**
     * 用户中心获取租车订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_index_rent_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $order_rent_table = $this->db->dbprefix("order_rent");
        $car_info_table = $this->db->dbprefix("car_info");
        $car_price_table = $this->db->dbprefix("car_price");
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id,' . $table . '.order_note');
        $this->db->select($car_info_table . '.title,' . $car_info_table . '.car_type,' . $car_info_table . '.brand,' . $car_info_table . '.title,' . $car_price_table . '.cate_name,' . $car_price_table . '.cate_attr,' . $order_rent_table . '.go_date,' . $order_rent_table . '.price,' . $car_price_table . '.id as price_id,' . $car_price_table . '.price_unit');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($table, $table . ".order_id=$order_rent_table.order_id", 'inner');
        $this->db->join($car_price_table, $car_price_table . ".id=$order_rent_table.price_id", 'inner');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($car_info_table, $order_rent_table . ".car_id=$car_info_table.id", 'inner');
        if($where){
            $where .= " AND $order_rent_table.sid= ".SITEID .' ';
        }else{
            $where .= " $order_rent_table.sid= ".SITEID .' ';
        }
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($order_rent_table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/adminrent/edit?id=' . $row['order_id'] . '&price_id=' . $row['price_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];
            $row['car_url'] = site_url('jingdian/' . $row['scenic'] . '.html');

            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {
                $row['verify_url'] = site_url('order/admin/ajax_order_status?order_id=' . $row['order_id']);
            }

            $data[] = $row;
        }
        return $data;
    }

    /**
     * 获取租车订单总数量
     * @param string $where 查询条件
     * @return mixed
     */
    public function get_rent_count($where = '')
    {
        $table = $this->_get_table_name();
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }


    

    /**
     * 用户中心获取特色商品订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_index_goods_list($page_size = 20, $now_page = 1, $where)
    {
        $table = $this->_get_table_name();
        $order_goods_table = $this->db->dbprefix("order_goods");
        $goods_table = $this->db->dbprefix("goods");
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.cat_type,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($goods_table . '.goods_name,' . $goods_table . '.goods_id,' . $order_goods_table . '.num');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($table, $table . ".order_id=$order_goods_table.order_id", 'inner');
        $this->db->join($goods_table, $goods_table . ".goods_id=$order_goods_table.goods_id", 'inner');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($table . '.add_time', 'DESC');
        $query = $this->db->get($order_goods_table, $page_size, ($now_page - 1) * $page_size);
        $data = array();
        $status = $this->_set_order_status_name();
        foreach ($query->result_array() as $row) {
            $row['edit_url'] = site_url('order/index/goods_info?id=' . $row['order_id']);
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $row['cat_type_name'] = $status['cat_type'][$row['cat_type']];
            $row['goods_url'] = site_url('goods/' . $row['goods_id'] . '.html');

            //订单为审核状态时显示审核功能
            if ($row['order_status'] == 1) {
                $row['verify_url'] = site_url('order/admin/ajax_order_status?id=' . $row['order_id']);
            }
            if ($row['order_status'] != 1 && $row['pay_status'] != 3) {
                $row['pay_url'] = site_url('order/admin/ajax_pay_account?id=' . $row['order_id']);
            }
            $data[] = $row;
        }
        return $data;
    }


    /**
     * 获取订单状态 ，支付状态
     */
    public function _set_order_status_name()
    {
        $pay_status = array('', '未支付', '部分支付', '支付完成');
        $order_status = array('', '未审核', '已审核', '已取消', '已完成');
        $cat_type = array('', '门市订单', '线上订单', '掌旅宝订单', '团购订单', '', '手机订单', '', '分销商订单','', '同业订单');
        $data = array('order_status' => $order_status, 'pay_status' => $pay_status, 'cat_type' => $cat_type);
        return $data;
    }

    /**
     * 获取数据表名称
     */
    private function _get_table_name()
    {
        $table = $this->db->dbprefix("order_info");
        return $table;
    }

    function kouwei($data)
    {
        $sql = "update " . $this->db->dbprefix("line_go") . " set en_num=en_num+".intval($data['total_num'])." where go_id=".$data['go_id'];
        $res = $this->db->query($sql);
        $data['kouwei'] = isset($data['kouwei']) ? intval($data['kouwei']) : 1;
        if ($res) {
            return $this->edit_row(array('kouwei' => $data['kouwei']), array('order_id' => $data['order_id']));
        }
        return $res;

    }

    /**
     * 操作订单状态
     * $data['order_id'] 订单ID
     * $data['order_status'] 订单状态
     * $data['user_type'] 1为管理员  2为会员名称
     * @param $data
     * @return bool
     */
    public function translate_order_status($data)
    {
        $order_id = $data['order_id'];
        if (!isset($data['note'])) {
            $data['note'] = '操作订单状态';
        }
        $row = $this->Orderinfo_model->get_row(array('order_id' => $order_id));
        if (empty($row)) {
            return false;
        } else {
            $status = $this->Orderinfo_model->edit_row(array('order_status' => $data['order_status']), array('order_id' => $order_id));
            if ($status) {
                $action_user = $this->session->userdata('username');
                if (isset($data['user_type']) && $data['user_type'] == 2) $action_user = $this->session->userdata('identity_sign');
                $order_log = array(
                    'order_id' => $row['order_id'],
                    'order_status' => $data['order_status'],
                    'pay_status' => $row['pay_status'],
                    'note' => $data['note'],
                    'add_time' => date('Y-m-d H:i:s', now()),
                    'action_user' => $action_user,
                    'sid' => $this->sid,
                );
                $order_status = $this->Orderlog_model->add_row($order_log);
                if ($order_status) {
                    return $row['order_type'];
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }



   


    function order_list_count($where)
    {
        $sql = "select count(*) as num from " . $this->table_order_go . " as a left join " . $this->table_order_info . " as b on b.order_id=a.order_id";
        $sql .= " left join " . $this->table_admin_user . " as c on c.id=b.admin_user_id ";
        $sql .= " left join " . $this->table_line . " as d on d.line_id=a.line_id ";
        $sql .= " where a.sid=" . $this->sid . " and b.order_status<>3 ";
        $sql .= $this->to_sql($where);
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row['num'];
    }

    /**
     *
     * 统计总计结果
     * @param $where
     */
    function order_list_total($where)
    {
        $sql = "select a.financial_account as financial_account_total,b.order_account as order_account_total,sum(e.pay_account) as paid_account_total,a.cb_account as cb_account_total  ";
        $sql .= " from " . $this->table_order_go . " as a ";
        $sql .= " left join " . $this->table_order_info . " as b on b.order_id=a.order_id";
        $sql .= " left join " . $this->table_admin_user . " as c on c.id=b.admin_user_id ";
        $sql .= " left join " . $this->table_line . " as d on d.line_id=a.line_id ";
        $sql .= " left join " . $this->table_order_pay . " as e on e.order_id=b.order_id and e.status=2 and e.type=1 ";
        $sql .= " where a.sid=" . $this->sid . " and b.order_status<>3 ";
        $sql .= $this->to_sql($where);
        $sql .= " group by a.order_id";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $row = array('financial_account_total' => 0, 'order_account_total' => 0, 'paid_account_total' => 0, 'cb_account_total' => 0);
        foreach ($res as $key => $val) {
            $row['financial_account_total'] += $val['financial_account_total'];
            $row['order_account_total'] += $val['order_account_total'];
            $row['paid_account_total'] += $val['paid_account_total'];
            $row['cb_account_total'] += $val['cb_account_total'];
        }
        return $row;
    }

    function order_list_download($where)
    {
        $sql = "select a.order_id,a.go_id,a.go_date,a.line_id,a.line_name,a.line_number,a.total_num,a.financial_account,a.cost_yewu,a.cost_qiantai,a.cost_baoxian,a.cost_qita,b.paid_supplier,b.commission,b.cat_type,b.order_sn,b.order_account,b.paid_account from " . $this->table_order_go . " as a ";
        $sql .= " left join " . $this->table_order_info . " as b on b.order_id=a.order_id ";
        $sql .= " left join " . $this->table_admin_user . " as c on c.id=b.admin_user_id ";
        $sql .= " left join " . $this->table_line . " as d on d.line_id=a.line_id ";
        $sql .= " where a.sid=" . $this->sid . " and b.order_status<>3 ";
        $sql .= $this->to_sql($where);
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    /**
     *
     * 统计汇总订单列表
     */
    function order_list($page_size = 20, $now_page = 1, $where)
    {
        $sql = "select a.order_id,a.go_id,a.cb_account,a.go_date,a.line_id,a.line_name,a.line_number,a.total_num,a.financial_account,a.cost_yewu,a.cost_qiantai,a.cost_baoxian,a.cost_qita,b.paid_supplier,b.commission,b.cat_type,b.order_sn,b.order_account,c.name as admin_user_name from " . $this->table_order_go . " as a ";
        $sql .= " left join " . $this->table_order_info . " as b on b.order_id=a.order_id ";
        $sql .= " left join " . $this->table_admin_user . " as c on c.id=b.admin_user_id ";
        $sql .= " left join " . $this->table_line . " as d on d.line_id=a.line_id ";
        //$sql.= " left join ".$this->table_order_pay." as l on l.order_id=a.order_id and l.status=1 and l.type=1 ";
        $sql .= " where a.sid=" . $this->sid . " and b.order_status<>3 ";
        $sql .= $this->to_sql($where);
        $start = $page_size * ($now_page - 1);
        $sql .= " limit $start,$page_size";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        foreach ($res as $key => $val) {
            $res[$key]['paid_account'] = $this->Orderpaylog_model->get_pay_account(array('order_id' => $val['order_id'], 'status' => 2, 'type' => 1));
        }
        return $res;

    }

    /**
     *
     * go_id 下的订单数
     * @param $go_id
     */
    function count_by_go($go_id)
    {
        $sql = "select count(*) as num from " . $this->table_order_go . " where go_id=$go_id";
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row['num'];
    }

    /**
     *
     * Enter description here ...
     * @param $go_id
     */
    function pay_list($go_id)
    {
        $sql = "select * from " . $this->table_go_pay . " where go_id=$go_id";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    /**
     *
     * 添加团队付款
     * @param $data
     */
    function add_go_pay($data)
    {
        if (is_array($data)) {
            if ($this->db->insert($this->table_go_pay, $data)) {
                return $this->db->insert_id();
            } else {
                return false;
            }
        }
    }

    private function to_sql($where)
    {
        $sql = '';
        //权限的判断
        $auth = $this->session->userdata('authority');
        $admin_id = $this->session->userdata('admin_user_id');
        if ($auth) {
            $sql .= " and (c.division_id in ($auth) or b.cat_type<>1 or c.id=" . $admin_id . " ) ";
        } else {
            $sql .= " and (b.cat_type<>1 or c.id=" . $admin_id . " ) ";
        }
        if (!empty($where['order_sn'])) {
            $sql .= " and b.order_sn like '%" . $where['order_sn'] . "%' ";
        }
        if (!empty($where['line'])) {
            $sql .= " and a.line_name like '%" . $where['line'] . "%' ";
        }
        if (!empty($where['start_date'])) {
            $sql .= " and a.go_date >= '" . $where['start_date'] . "' ";
        }
        if (!empty($where['end_date'])) {
            $sql .= " and a.go_date <= '" . $where['end_date'] . "' ";
        }
        if (!empty($where['start_date_order'])) {
            $sql .= " and b.add_time >= '" . $where['start_date_order'] . "' ";
        }
        if (!empty($where['end_date_order'])) {
            $sql .= " and b.add_time <= '" . $where['end_date_order'] . "' ";
        }

        if (!empty($where['cat_type'])) {
            $sql .= " and b.cat_type = " . $where['cat_type'];
            if ($where['cat_type'] != 8) {
                $sql .= " and b.cat_type<>8 ";
            }
        }

        if (!empty($where['division_id'])) {
            $sql .= " and c.division_id in (select id from " . $this->table_division . " where find_in_set($where[division_id],code))";
        }
        if (!empty($where['supplier_id'])) {
            $sql .= " and d.supplier_id=$where[supplier_id]";
        }
        if (!empty($where['admin_user_id'])) {
            $sql .= " and b.admin_user_id=$where[admin_user_id]";
        }
        if (!empty($where['admin_user_name'])) {
            $sql .= " and c.name='" . $where[admin_user_name] . "'";
        }
        if (@$where['pay_status'] == 5) {
            $sql .= " and b.pay_status<3 ";
        }
        if ($where['line_sort']) {
            $sql .= " and d.line_sort=" . $where['line_sort'];//线路归属1自主2代卖
        }
        return $sql;
    }


    /**
     * @param int $page_size
     * @param int $now_page
     * @param $where
     * 分销订单列表  -zj
     */
    function dis_order_list($page_size = 20, $now_page = 1, $where)
    {
        $sql = "select a.order_id,a.go_id,a.go_date,a.line_name,a.line_number,a.total_num,b.order_account,e.supplier_name,c.name as admin_user_name,h.heji,b.finacial_status,b.order_sn,
b.pay_status from $this->table_order_go as a ";
        $sql .= " left join $this->table_order_info as b on b.order_id=a.order_id ";
        $sql .= " left join " . $this->table_admin_user . " as c on c.id=b.admin_user_id ";
        $sql .= " left join " . $this->table_line . " as d on d.line_id=a.line_id ";
        $sql .= " left join " . $this->table_supplier . " as e on e.supplier_id=d.supplier_id ";
        $sql .= " left join $this->table_hetong as h on h.order_id=b.order_id ";
        $sql .= " where b.sid=$this->sid and b.cat_type=8 and b.order_status<>3 ";
        if (!empty($where['supplier_id'])) {
            $sql .= " and d.supplier_id=$where[supplier_id] ";
        }
        if (!empty($where['order_sn'])) {
            $sql .= " and b.order_sn='" . $where['order_sn'] . "'";
        }
        if (!empty($where['pay_status'])) {
            $sql .= " and b.pay_status='" . $where['pay_status'] . "'";
        }
        if (!empty($where['finacial_status'])) {
            $sql .= " and b.finacial_status='" . $where['finacial_status'] . "'";
        }
        if (!empty($where['line'])) {
            $sql .= " and (a.line_name like '%" . $where['line'] . "%' or a.line_number like '%" . $where['line'] . "%') ";
        }
        if (!empty($where['start_date'])) {
            $sql .= " and a.go_date >= '" . $where['start_date'] . "' ";
        }
        if (!empty($where['end_date'])) {
            $sql .= " and a.go_date <= '" . $where['end_date'] . "' ";
        }
        if (!empty($where['admin_user_name'])) {
            $sql .= " and c.name='" . $where['admin_user_name'] . "'";
        }

        if(!empty($where['start_add'])){
            $sql .= " and  b.add_time>='".$where['start_add']."'";
        }
        if(!empty($where['end_add'])){
            $sql .= " and  b.add_time<'".date('Y-m-d',strtotime($where['end_add'])+3600*24)."'";
        }

        $query_num = $this->db->query($sql);
        $total = $query_num->num_rows();
        $start = ($now_page - 1) * $page_size;
        $sql .= " order by a.go_date limit $start,$page_size ";
        $query = $this->db->query($sql);
        $list = $query->result_array();

        return array('total' => $total, 'list' => $list);
    }


    function dis_order_total($where)
    {
        $sql = "select sum(b.order_account) as total_account, sum(a.total_num) as total_num, sum(a.total_num_ch) as total_num_ch,sum(h.heji) as total_heji from $this->table_order_go as a ";
        $sql .= " left join $this->table_order_info as b on b.order_id=a.order_id ";
        $sql .= " left join " . $this->table_admin_user . " as c on c.id=b.admin_user_id ";
        $sql .= " left join " . $this->table_line . " as d on d.line_id=a.line_id ";
        $sql .= " left join " . $this->table_supplier . " as e on e.supplier_id=d.supplier_id ";
        $sql .= " left join $this->table_hetong as h on h.order_id=b.order_id ";
        $sql .= " where b.sid=$this->sid and b.cat_type=8 and b.order_status<>3 ";
        if (!empty($where['supplier_id'])) {
            $sql .= " and d.supplier_id=$where[supplier_id] ";
        }
        if(!empty($where['start_add'])){
            $sql .= " and  b.add_time>='".$where['start_add']."'";
        }
        if(!empty($where['end_add'])){
            $sql .= " and  b.add_time<'".date('Y-m-d',strtotime($where['end_add'])+3600*24)."'";
        }
        if (!empty($where['order_sn'])) {
            $sql .= " and b.order_sn='" . $where['order_sn'] . "'";
        }
        if (!empty($where['pay_status'])) {
            $sql .= " and b.pay_status='" . $where['pay_status'] . "'";
        }
        if (!empty($where['finacial_status'])) {
            $sql .= " and b.finacial_status='" . $where['finacial_status'] . "'";
        }
        if (!empty($where['line'])) {
            $sql .= " and (a.line_name like '%" . $where['line'] . "%' or a.line_number like '%" . $where['line'] . "%') ";
        }
        if (!empty($where['start_date'])) {
            $sql .= " and a.go_date >= '" . $where['start_date'] . "' ";
        }
        if (!empty($where['end_date'])) {
            $sql .= " and a.go_date <= '" . $where['end_date'] . "' ";
        }
        if (!empty($where['admin_user_name'])) {
            $sql .= " and c.name='" . $where['admin_user_name'] . "'";
        }

        $sql .= " ";

        $query = $this->db->query($sql);
        $res = $query->row_array();
        return $res;
    }

    function get_hetong_order($order_id)
    {
        $sql = "select a.order_sn,b.go_date,a.add_time,c.group_num,d.travel_days,d.travel_nights,a.order_id,e.cat_id from $this->table_order_info as a left join $this->table_order_go as b on b.order_id=a.order_id";
        $sql .= " left join $this->table_line_go as c on c.go_id=b.go_id left join $this->table_line_info as d on d.line_id=b.line_id left join $this->table_line as e on e.line_id=b.line_id  where a.order_id=$order_id";
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row;
    }

    /**
     * 获取订单数量
     * @param $where 数组 查询条件
     */
    public function get_order_count($where)
    {
        $table = $this->_get_table_name();
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }

    /**
     * 获取订单图表数据
     * @param string $format 数据格式
     * @param $where 查询条件
     * @return mixed
     */
    public function order_chart($format = "'%Y%m%d'", $where = '')
    {
        $sql = "select DATE_FORMAT(add_time,$format) as date,count(*) as order_num from " . $this->table_order_info;
        $sql .= " WHERE from_sid=" . $this->sid;
        $sql .= $where ? " AND $where" : '';
        $sql .= " group by DATE_FORMAT(add_time,$format) ORDER BY date ASC";
        $query = $this->db->query($sql);
        $list = $query->result_array();
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $list[$key]['order_num'] = intval($val['order_num']);
            }
        }
        return $list;
    }


    /**
     * 返佣操作
     *  @return mixed
     */
    public function back_order_list(){
        $now = time();
        $sql = " select o.order_sn,o.order_id,o.back_account,a.go_date,b.travel_days,o.admin_user_id from $this->table_order_info as o";
        $sql.= " left join $this->table_order_go as a on a.order_id=o.order_id left join $this->table_line_info as b on b.line_id=a.line_id ";
        $sql.= " where o.back_status=0 and back_account>0 and o.order_status<>3 and o.pay_status=3 and o.admin_user_id>0 and o.back_status=0 and a.go_date<'".date("Y-m-d")."' ";
        $query = $this->db->query($sql);
        $list = $query->result_array();
        $order_ids = '0';
        foreach($list as $key=>$val){
            if(strtotime($val['go_date'])+86400<$now){
                $data = array('cash_type'=>3,
                    'type'=>1,
                    'sid'=>$this->sid,
                    'money'=>$val['back_account'],
                    'remark'=>$val['order_sn'],
                    'add_time'=>date('Y-m-d H:i:s'),
                    'user_id'=>$val['admin_user_id'],
                );
                $res = $this->Adminuser_model->add_cash_log($data);
                if($res){
                    $order_ids.= ','.$val['order_id'];
                }
            }
        }
        $result = $this->db->query("update $this->table_order_info set back_status=1 where order_id in($order_ids)");
        return $result;
    }


    /**
     * 订单按月份汇总，订单数量，订单金额，订单人数
     */
    public function order_summary(){
        $month = array('2014-10','2014-11','2014-12','2015-01','2015-02','2015-03','2015-04','2015-05','2015-06','2015-07','2015-08','2015-09','2015-10','2015-11','2015-12');


    }

    public function get_by_lineid_userid($user_id,$line_id)
    {

        $sql = "select line_id from " . $this->table_order_go . " as a left join " . $this->table_order_info . " as b on b.order_id=a.order_id";
        $sql .= " where a.sid=" . $this->sid . " and a.line_id=$line_id AND b.user_id=$user_id LIMIT 1 ";
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row;
    }
}